package com.webframe.web.controller;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.mlw.vlh.ValueList;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.webframe.po.HouseQueryConditionBean;
import com.webframe.service.def.IHouseInfoRentPubService;
import com.webframe.service.def.IHouseRentQueryService;
import com.webframe.util.WordToPinyinUtil;

/**
 *租房信息查询
 * Created on 2013-10-14-下午09:14:26
 * @author xiaokui.li
 */
@Controller
@RequestMapping("/houseRentQuery")
public class HouseRentQueryController extends BaseController
{
	@Autowired
	IHouseRentQueryService houseRentQueryService;
	@Autowired
	IHouseInfoRentPubService houseInfoRentPubService;
	@Autowired
	private ValueListController valueListController;
	
	@SuppressWarnings("rawtypes")
	@RequestMapping("/realEstate/index")
	public String index(HttpServletRequest request,HttpServletResponse response, HouseQueryConditionBean hqc) throws Exception
	{
		//保存筛选条件
		StringBuilder siftSb = new StringBuilder();
		
		if(StringUtils.isEmpty(hqc.getCityId()) || StringUtils.isEmpty(hqc.getCityName()))
		{
			return "redirect:/realEstate/home.wf";
		}
		
		//查询对应城市包含的区域
		List<Map<String, Object>> districtList = houseInfoRentPubService.getDistrict(hqc.getCityId());
		String districtStr = getDistrict(districtList, hqc.getCityId(), hqc.getCityName(), hqc.getDistrictId(), siftSb);
		//查询子区域
		String businessStr = "";
		if(StringUtils.isNotEmpty(hqc.getDistrictId()))
		{
			List<Map<String, Object>> businessList = houseInfoRentPubService.getDistrict(hqc.getDistrictId());
			businessStr = getBusiness(businessList, hqc.getDistrictId(), hqc.getBusinessId(), siftSb);
		}
		//查询租金范围
		List<Map<String, Object>> rentalList = houseRentQueryService.getRental(null);
		String rentalStr = getRental(rentalList, hqc.getRentalId(), siftSb);
		if(StringUtils.isEmpty(hqc.getRentalId()) && (StringUtils.isNotEmpty(hqc.getRentalIdLow())||StringUtils.isNotEmpty(hqc.getRentalIdHigh())))
		{
			String rangeStr = "";
			if(StringUtils.isEmpty(hqc.getRentalIdHigh()))
			{
				rangeStr = "筛选：大于或等于" + hqc.getRentalIdLow();
			}
			else
			{
				rangeStr = "筛选：" + hqc.getRentalIdLow()+"-"+hqc.getRentalIdHigh();
			}
			siftSb.append("<li>"+rangeStr+"<a class=\"ui-search-filter-close\" href=\"#1\" onclick=\"removeCondition(this,'rentalId');\"></a></li>");
		}
		//查询房型
		List<Map<String, Object>> houseTypeList = houseRentQueryService.getHouseType(null);
		String houseTypeStr = getHouseType(houseTypeList, hqc.getHouseTypeId(), siftSb);
		//查询住房类型
		List<Map<String, Object>> residenceTypeList = houseInfoRentPubService.getResidenceType(null);
		String residenceStr = genMoreOpts(residenceTypeList, "residenceTypeId", hqc.getResidenceTypeId(), hqc.getResidenceTypeIdLabel(), "类型", siftSb);
		//装修类型
		List<Map<String, Object>> adornTypeList = houseInfoRentPubService.getAdornType(null);
		String adornTypeStr = genMoreOpts(adornTypeList, "adornTypeId", hqc.getAdornTypeId(), hqc.getAdornTypeIdLabel(), "装修", siftSb);
		//楼层分类
		List<Map<String, Object>> floorTypeList = houseRentQueryService.getFloorType(null);
		String floorTypeStr = genMoreOpts(floorTypeList, "floorTypeId", hqc.getFloorTypeId(), hqc.getFloorTypeIdLabel(), "楼层", siftSb);
		//朝向
		List<Map<String, Object>> directionList = houseInfoRentPubService.getDirection(null);
		String directionStr = genMoreOpts(directionList, "directionId", hqc.getDirectionId(), hqc.getDirectionIdLabel(), "朝向", siftSb);

		//查询
		Map params = new HashMap();
		if(StringUtils.isNotEmpty(hqc.getHouseTypeId()))
		{
			for (Iterator iterator = houseTypeList.iterator(); iterator.hasNext();)
			{
				Map<String, Object> map = (Map<String, Object>) iterator.next();
				if(hqc.getHouseTypeId().equals(map.get("id_").toString()))
				{
					params.put("houseTypeOp", map.get("op_"));
				}
			}
		}
		if(StringUtils.isNotEmpty(hqc.getFloorTypeId()))
		{
			for (Iterator iterator = floorTypeList.iterator(); iterator.hasNext();)
			{
				Map<String, Object> map = (Map<String, Object>) iterator.next();
				if(hqc.getFloorTypeId().equals(map.get("id_").toString()))
				{
					params.put("floorTypeOp", map.get("op_"));
				}
			}
		}
		ValueList houseInfoList = valueListController.getValueList("houseRentQueryAdapter", request, params);
		genHouseRentInfo(houseInfoList);
		
		request.setAttribute("rentalStr", rentalStr);
		request.setAttribute("houseTypeStr", houseTypeStr);
		request.setAttribute("districtStr", districtStr);
		request.setAttribute("businessStr", businessStr);
		request.setAttribute("residenceStr", residenceStr);
		request.setAttribute("adornTypeStr", adornTypeStr);
		request.setAttribute("floorTypeStr", floorTypeStr);
		request.setAttribute("directionStr", directionStr);
		request.setAttribute("districtId", hqc.getDistrictId());
		request.setAttribute("businessId", hqc.getBusinessId());
		request.setAttribute("rentalId", hqc.getRentalId());
		request.setAttribute("rentalIdLow", hqc.getRentalIdLow());
		request.setAttribute("rentalIdHigh", hqc.getRentalIdHigh());
		if(StringUtils.isEmpty(hqc.getRentalId()))
		{
			if(StringUtils.isNotEmpty(hqc.getRentalIdLow()))
			{
				request.setAttribute("myPriceLow", hqc.getRentalIdLow());
			}
			if(StringUtils.isNotEmpty(hqc.getRentalIdHigh()))
			{
				request.setAttribute("myPriceHigh", hqc.getRentalIdHigh());
			}
				
		}
		request.setAttribute("houseTypeId", hqc.getHouseTypeId());
		request.setAttribute("residenceTypeId", hqc.getResidenceTypeId());
		request.setAttribute("adornTypeId", hqc.getAdornTypeId());
		request.setAttribute("floorTypeId", hqc.getFloorTypeId());
		request.setAttribute("directionId", hqc.getDirectionId());
		request.setAttribute("residenceTypeIdLabel", hqc.getResidenceTypeIdLabel());
		request.setAttribute("adornTypeIdLabel", hqc.getAdornTypeIdLabel());
		request.setAttribute("floorTypeIdLabel", hqc.getFloorTypeIdLabel());
		request.setAttribute("directionIdLabel", hqc.getDirectionIdLabel());
		request.setAttribute("siftStr", siftSb.toString());
		
		return "/realEstate/houseRentQuery";
	}
	
	/**
	 * 组装区域字符串
	 * @param districtList
	 * @return
	 */
	private String getDistrict(List<Map<String, Object>> districtList, String cityId, String cityName, String districtId, StringBuilder siftSb)
	{
		StringBuilder districtStr = new StringBuilder();
		districtStr.append("<a href=\"javascript:void(0);\" onclick=\"districtQuery();\" class=\"first-vm a-circle")
					  .append(StringUtils.isEmpty(districtId) ? " a-circle-on" : "")
					  .append("\" title=\"全"+cityName+"\" ><em>全"+cityName+"</em></a>");
		for (Iterator<Map<String, Object>> iterator = districtList.iterator(); iterator.hasNext();)
		{
			Map<String, Object> map = iterator.next();
			districtStr.append("<a rel=\"nofollow\" href=\"javascript:void(0);\" onclick=\"districtQuery('"+map.get("id_")+"')\" class=\"a-area");
			if(map.get("id_").toString().equals(districtId))
			{
				districtStr.append(" a-area-on");
				siftSb.append("<li>"+map.get("name_")+"<a class=\"ui-search-filter-close\" href=\"#1\" onclick=\"removeCondition(this,'districtId');\"></a></li>");
			}
			districtStr.append("\">" + map.get("name_") + "</a>");
		}
		return districtStr.toString();
	}
	
	/**
	 * 拼装子区域
	 * @param businessList
	 * @param districtId
	 * @param businessId
	 * @return
	 */
	private String getBusiness(List<Map<String, Object>> businessList, String districtId, String businessId, StringBuilder siftSb)
	{
		StringBuilder businessStr = new StringBuilder();
		businessStr.append("<div class=\"subarea\">");
		businessStr.append("<a href=\"javascript:void(0);\" onclick=\"businessQuery();\" class=\"a-circle")
					  .append(StringUtils.isEmpty(businessId) ? " a-circle-on" : "")
					  .append("\" title=\"全部\" ><em>全部</em></a>");
		Map<String, Map<String, String>> busiMap = new TreeMap<String, Map<String,String>>();
		for (Iterator<Map<String, Object>> iterator = businessList.iterator(); iterator.hasNext();)
		{
			Map<String, Object> map = iterator.next();
			String headChar = ("" + WordToPinyinUtil.getPinYinHeadChar(map.get("name_").toString()).charAt(0)).toUpperCase();
			if(busiMap.get(headChar) == null)
			{
				Map<String, String> bMap = new HashMap<String, String>();
				bMap.put(map.get("id_").toString(), map.get("name_").toString());
				busiMap.put(headChar, bMap);
			}
			else
			{
				busiMap.get(headChar).put(map.get("id_").toString(), map.get("name_").toString());
			}
		}
		Set<Entry<String, Map<String, String>>> busiEntrySet = busiMap.entrySet();
		for (Iterator<Entry<String, Map<String, String>>> iterator = busiEntrySet.iterator(); iterator.hasNext();)
		{
			Entry<String, Map<String, String>> entry = iterator.next();
			businessStr.append("<b class='area-letter'>"+entry.getKey()+"</b>");
			Map<String, String> bMap = entry.getValue();
			Set<Entry<String, String>> subBusiEntrySet = bMap.entrySet();
			for (Iterator<Entry<String, String>> iterator2 = subBusiEntrySet.iterator(); iterator2.hasNext();)
			{
				Entry<String, String> entry2 = iterator2.next();
				businessStr.append("<a rel=\"nofollow\" href=\"javascript:void(0)\" onclick=\"businessQuery('" + entry2.getKey() + "')\" class=\"a-circle");
				if(entry2.getKey().toString().equals(businessId))
				{
					if(StringUtils.isNotEmpty(districtId))
					{
						siftSb.delete(0, siftSb.length());
					}
					siftSb.append("<li>"+entry2.getValue()+"<a class=\"ui-search-filter-close\" href=\"#1\" onclick=\"removeCondition(this,'businessId');\"></a></li>");
					businessStr.append(" a-circle-on");
				}
				businessStr.append("\">" + entry2.getValue() + "</a> ");
			}
		}
		businessStr.append("</div>");
		return businessStr.toString();
	}
	
	/**
	 * 组装租金范围
	 * @param rentalList
	 * @return
	 */
	private String getRental(List<Map<String, Object>> rentalList, String rentalId, StringBuilder siftSb)
	{
		StringBuilder rentalStr = new StringBuilder();
		rentalStr.append("<a href=\"javascript:void(0);\" onclick=\"rentalQuery('')\" class=\"a-circle")
		.append(StringUtils.isEmpty(rentalId)?" a-circle-on":"")
		.append("\" title=\"不限\"><em>不限</em></a>");
		for (Iterator<Map<String, Object>> iterator = rentalList.iterator(); iterator.hasNext();)
		{
			Map<String, Object> map = iterator.next();
			rentalStr.append("<a rel=\"nofollow\" href=\"javascript:void(0);\" onclick=\"rentalQuery('"+map.get("id_")+"','"+map.get("low_")+"','"+map.get("high_")+"')\" class=\"a-circle");
			if(map.get("id_").toString().equals(rentalId))
			{
				siftSb.append("<li>"+map.get("name_")+"<a class=\"ui-search-filter-close\" href=\"#1\" onclick=\"removeCondition(this,'rentalId');\"></a></li>");
				rentalStr.append(" a-circle-on");
			}
			rentalStr.append("\"><em>" + map.get("name_") + "</em></a>");
		}
		return rentalStr.toString();
	}
	
	/**
	 * 组装房型
	 * @param houseTypeList
	 * @return
	 */
	private String getHouseType(List<Map<String, Object>> houseTypeList, String houseTypeId, StringBuilder siftSb)
	{
		StringBuilder houseTypeStr = new StringBuilder();
		houseTypeStr.append("<a href=\"javascript:void(0);\" onclick=\"houseTypeQuery('');\" class=\"a-circle")
		.append(StringUtils.isEmpty(houseTypeId)?" a-circle-on":"")
		.append("\" title=\"不限\"><em>不限</em></a>");
		for (Iterator<Map<String, Object>> iterator = houseTypeList.iterator(); iterator.hasNext();)
		{
			Map<String, Object> map = iterator.next();
			houseTypeStr.append("<a rel=\"nofollow\" href=\"javascript:void(0);\" onclick=\"houseTypeQuery('"+map.get("id_")+"')\"  class=\"a-circle");
			if(map.get("id_").toString().equals(houseTypeId))
			{
				siftSb.append("<li>"+map.get("name_")+"<a class=\"ui-search-filter-close\" href=\"#1\" onclick=\"removeCondition(this,'houseTypeId');\"></a></li>");
				houseTypeStr.append(" a-circle-on");
			}
			houseTypeStr.append("\">"+map.get("name_")+"</a>");
		}
		return houseTypeStr.toString();
	}
	
	/**
	 * 组装更多查询条件
	 * @param moreOptsList
	 * @return
	 */
	private String genMoreOpts(List<Map<String, Object>> moreOptsList, String type, String id, String label, String defaultValue, StringBuilder siftSb)
	{
		StringBuilder moreOptsStr = new StringBuilder();
		moreOptsStr.append("<div class=\"moreitem-btn\"><a href=\"#1\">")
		.append(StringUtils.isEmpty(label) ? defaultValue : label)
		.append("</a></div>")
		.append("<ul class=\"moreitem-opts\">")
		.append("<li><a href=\"javascript:void(0);\" onclick=\"moreOptsQuery('" + type + "','','');\">不限</a></li>");
		
		for (Iterator<Map<String, Object>> iterator = moreOptsList.iterator(); iterator.hasNext();)
		{
			Map<String, Object> map = iterator.next();
			moreOptsStr.append("<li><a href=\"javascript:void(0);\" onclick=\"moreOptsQuery('" + type + "','"+map.get("id_")+"','"+map.get("name_")+"');\">" + map.get("name_") + "</a></li>");
			if(map.get("id_").equals(id))
			{
				siftSb.append("<li>"+map.get("name_")+"<a class=\"ui-search-filter-close\" href=\"#1\" onclick=\"removeCondition(this,'"+type+"', true);\"></a></li>");
			}
		}
		moreOptsStr.append("</ul>");
		return moreOptsStr.toString();
	}
	/**
	 * 组装租房信息
	 */
	private String genHouseRentInfo(ValueList list)
	{
		StringBuilder houseRentInfoStr = new StringBuilder();
		List list1 = list.getList();
		for (Iterator iterator = list1.iterator(); iterator.hasNext();)
		{
			
			
		}
		return houseRentInfoStr.toString();
	}
}